<template>
  <div>
    <el-form
      style="margin-top: 20px"
      :model="form"
      ref="ruleForm"
      label-width="150px"
    >
      <el-form-item label="自动重启时间" style="min-width: 1600px">
        <el-col :span="4">
          <el-select
            v-model="form.date"
            multiple
            collapse-tags
            placeholder="请选择自动重启时间"
          >
            <el-option
              v-for="item in optionList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-col>
        <el-col class="line" :span="1">在</el-col>
        <el-col :span="4">
          <el-time-picker
            v-model="form.sTime"
            value-format="HH:mm:ss"
            format="HH:mm:ss"
            placeholder="请选择时间点"
          >
          </el-time-picker>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="getRebootStrategy">取消</el-button>
      </el-form-item>
      <el-form-item label="手动重启设备">
        <el-button type="primary" @click="reboot">重启设备</el-button>
      </el-form-item>
      <el-form-item>重启完毕后页面将重新载入</el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getRebootStrategyApi,
  editRebootStrategyApi,
  rebootyApi,
} from "@/api/access/equipment";
export default {
  name: "MvCloudgateTerminalRestartDevice",

  data() {
    return {
      optionList: [
        {
          label: "不重启",
          value: "iNoRestart",
        },
        {
          label: "星期一",
          value: "iMonday",
        },
        {
          label: "星期二",
          value: "iTuesday",
        },
        {
          label: "星期三",
          value: "iWednesday",
        },
        {
          label: "星期四",
          value: "iThursday",
        },
        {
          label: "星期五",
          value: "iFriday",
        },
        {
          label: "星期六",
          value: "iSaturday",
        },
        {
          label: "星期日",
          value: "iSunday",
        },
      ],
      form: {
        date: "",
        sTime: "",
      },
    };
  },

  mounted() {
    this.getRebootStrategy();
  },

  methods: {
    getRebootStrategy() {
      getRebootStrategyApi().then((res) => {
        let list = [];
        let allZero = 0;
        Object.keys(res).forEach(i => {
          if (res[i] > 0) {
            allZero++;
          }
        })
        this.optionList.map((item) => {
          if (res[item.value] == "1") {
            list.push(item.value);
          }
        });
        if (allZero == 0) {
          list.push('iNoRestart');
        }
        this.form.date = list;
        this.form.sTime = res.sTime;
      });
    },
    onSubmit() {
      if (this.form.date.length > 1) {
        if (this.form.date.indexOf('iNoRestart') > -1) {
          let index = this.form.date.indexOf('iNoRestart');
          this.form.date.splice(index, 1);
        }
      } else if (this.form.date.length == 1) {
        if (this.form.date[0] == 'iNoRestart') {
          delete this.form.date;
        }
      } else {
        this.$message.error("自动重启时间不能为空");
        return false;
      }
      let params = {
        iMonday: 0,
        iTuesday: 0,
        iWednesday: 0,
        iThursday: 0,
        iFriday: 0,
        iSaturday: 0,
        iSunday: 0,
        sTime: this.form.sTime,
      };
      for (let key in this.form.date) {
        let attr = this.form.date[key];
        params[attr] = 1;
      }
      editRebootStrategyApi(params).then((res) => {
        console.log(res);
        this.$message.success("设置重启策略成功");
      });
    },
    reboot() {
      var that = this;
      this.$confirm("此操作将重启设备, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          rebootyApi().then((res) => {
            console.log(res);
            this.$store.dispatch("LogOut").then(() => {
              location.href = "/index";
            });
            that.$message.success("重启成功");
          });
        })
        .catch(() => {
          // this.$message.info("已取消重启");
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
